home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / glibmm-2.4 / glibmm / value.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-20  |  7.7 KB  |  320 lines

  1. // -*- c++ -*-
  2. #ifndef _GLIBMM_VALUE_H
  3. #define _GLIBMM_VALUE_H
  4. /* $Id: value.h,v 1.8 2005/01/21 19:26:04 murrayc Exp $ */
  5.  
  6. /* Copyright 2002 The gtkmm Development Team
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <glib-object.h>
  24. #include <glibmm/refptr.h>
  25. #include <glibmm/ustring.h>
  26.  
  27.  
  28. namespace Glib
  29. {
  30.  
  31. class ObjectBase;
  32. class Object;
  33.  
  34. /** @defgroup glibmmValue Generic Values
  35.  *
  36.  * Glib::Value<> is specialized for almost any type used within
  37.  * the glibmm and gtkmm libraries.
  38.  *
  39.  * - Basic types like <tt>int</tt>, <tt>char</tt>, <tt>bool</tt>, etc., also <tt>void*</tt>.
  40.  * - Glib::ustring and std::string.
  41.  * - Pointers to classes derived from Glib::Object.
  42.  * - Glib::RefPtr<> pointer types, which are assumed to be Glib::Object pointers.
  43.  * - All flags and enum types used within the gtkmm libraries.
  44.  *
  45.  * If a type doesn't fit into any of these categories, then a generic
  46.  * implementation for custom types will be used.  The requirements imposed
  47.  * on custom types are described in the Glib::Value class documentation.
  48.  */
  49.  
  50. /**
  51.  * @ingroup glibmmValue
  52.  */
  53. class ValueBase
  54. {
  55. public:
  56.   /** Initializes the GValue, but without a type.  You have to
  57.    * call init() before using the set(), get(), or reset() methods.
  58.    */
  59.   ValueBase();
  60.  
  61.   ValueBase(const ValueBase& other);
  62.   ValueBase& operator=(const ValueBase& other);
  63.  
  64.   ~ValueBase();
  65.  
  66.   /** Setup the GValue for storing the specified @a type.
  67.    * The contents will be initialized to the default value for this type.
  68.    * Note that init() should never be called twice.
  69.    *
  70.    * init() is not implemented as constructor, to avoid the necessity
  71.    * to implement a forward constructor in each derived class.
  72.    */
  73.   void init(GType type);
  74.  
  75.   /** Reset contents to the default value of its type.
  76.    */
  77.   void reset();
  78.  
  79.   GValue*       gobj()       { return &gobject_; }
  80.   const GValue* gobj() const { return &gobject_; }
  81.  
  82. protected:
  83.   GValue gobject_;
  84. };
  85.  
  86. /**
  87.  * @ingroup glibmmValue
  88.  */
  89. class ValueBase_Boxed : public ValueBase
  90. {
  91. public:
  92.   static GType value_type() G_GNUC_CONST;
  93.  
  94. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  95.   GParamSpec* create_param_spec(const Glib::ustring& name) const;
  96. #endif
  97.  
  98. protected:
  99.   void  set_boxed(const void* data);
  100.   void* get_boxed() const; // doesn't copy  
  101. };
  102.  
  103.  
  104. /**
  105.  * @ingroup glibmmValue
  106.  */
  107. class ValueBase_Object : public ValueBase
  108. {
  109. public:
  110.   static GType value_type() G_GNUC_CONST;
  111.  
  112. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  113.   GParamSpec* create_param_spec(const Glib::ustring& name) const;
  114. #endif
  115.  
  116. protected:
  117.   void set_object(Glib::ObjectBase* data);
  118.   Glib::ObjectBase* get_object() const;
  119.   Glib::RefPtr<Glib::ObjectBase> get_object_copy() const;
  120. };
  121.  
  122.  
  123. /**
  124.  * @ingroup glibmmValue
  125.  */
  126. class ValueBase_Enum : public ValueBase
  127. {
  128. public:
  129.   typedef gint CType;
  130.   static GType value_type() G_GNUC_CONST;
  131.  
  132. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  133.   GParamSpec* create_param_spec(const Glib::ustring& name) const;
  134. #endif
  135.  
  136. protected:
  137.   void set_enum(int data);
  138.   int  get_enum() const;
  139. };
  140.  
  141.  
  142. /**
  143.  * @ingroup glibmmValue
  144.  */
  145. class ValueBase_Flags : public ValueBase
  146. {
  147. public:
  148.   typedef guint CType;
  149.   static GType value_type() G_GNUC_CONST;
  150.  
  151. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  152.   GParamSpec* create_param_spec(const Glib::ustring& name) const;
  153. #endif
  154.  
  155. protected:
  156.   void set_flags(unsigned int data);
  157.   unsigned int get_flags() const;
  158. };
  159.  
  160.  
  161. /**
  162.  * @ingroup glibmmValue
  163.  */
  164. class ValueBase_String : public ValueBase
  165. {
  166. public:
  167.   typedef const gchar* CType;
  168.   static GType value_type() G_GNUC_CONST;
  169.  
  170. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  171.   GParamSpec* create_param_spec(const Glib::ustring& name) const;
  172. #endif
  173.  
  174. protected:
  175.   void set_cstring(const char* data);
  176.   const char* get_cstring() const; // never returns 0
  177. };
  178.  
  179. } // namespace Glib
  180.  
  181.  
  182. /* Include generic Glib::Value<> template, before any specializations:
  183.  */
  184. #define _GLIBMM_VALUE_H_INCLUDE_VALUE_CUSTOM_H
  185. #include <glibmm/value_custom.h>
  186. #undef _GLIBMM_VALUE_H_INCLUDE_VALUE_CUSTOM_H
  187.  
  188.  
  189. namespace Glib
  190. {
  191.  
  192. /**
  193.  * @ingroup glibmmValue
  194.  */
  195. template <class T>
  196. class Value_Boxed : public ValueBase_Boxed
  197. {
  198. public:
  199.   typedef T                           CppType;
  200.   typedef typename T::BaseObjectType* CType;
  201.  
  202.   static GType value_type() { return T::get_type(); }
  203.  
  204.   void set(const CppType& data) { set_boxed(data.gobj()); }
  205.   CppType get() const           { return CppType(static_cast<CType>(get_boxed())); }
  206. };
  207.  
  208. //More spec-compliant compilers (such as Tru64) need this to be near Glib::Object instead.
  209. #ifdef GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
  210.  
  211. /** Partial specialization for RefPtr<> to Glib::Object.
  212.  * @ingroup glibmmValue
  213.  */
  214. template <class T>
  215. class Value< Glib::RefPtr<T> > : public ValueBase_Object
  216. {
  217. public:
  218.   typedef Glib::RefPtr<T>             CppType;
  219.   typedef typename T::BaseObjectType* CType;
  220.  
  221.   static GType value_type() { return T::get_base_type(); }
  222.  
  223.   void set(const CppType& data) { set_object(data.operator->()); }
  224.   CppType get() const           { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
  225. };
  226.  
  227. //The SUN Forte Compiler has a problem with this: 
  228. #ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
  229.  
  230. /** Partial specialization for RefPtr<> to const Glib::Object.
  231.  * @ingroup glibmmValue
  232.  */
  233. template <class T>
  234. class Value< Glib::RefPtr<const T> > : public ValueBase_Object
  235. {
  236. public:
  237.   typedef Glib::RefPtr<const T>       CppType;
  238.   typedef typename T::BaseObjectType* CType;
  239.  
  240.   static GType value_type() { return T::get_base_type(); }
  241.  
  242.   void set(const CppType& data) { set_object(const_cast<T*>(data.operator->())); }
  243.   CppType get() const           { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
  244. };
  245. #endif //GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
  246.  
  247. #endif //GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
  248.  
  249. } // namespace Glib
  250.  
  251.  
  252. /* Include generated specializations of Glib::Value<> for fundamental types:
  253.  */
  254. #define _GLIBMM_VALUE_H_INCLUDE_VALUE_BASICTYPES_H
  255. #include <glibmm/value_basictypes.h>
  256. #undef _GLIBMM_VALUE_H_INCLUDE_VALUE_BASICTYPES_H
  257.  
  258.  
  259. namespace Glib
  260. {
  261.  
  262. /** Specialization for strings.
  263.  * @ingroup glibmmValue
  264.  */
  265. template <>
  266. class Value<std::string> : public ValueBase_String
  267. {
  268. public:
  269.   typedef std::string CppType;
  270.  
  271.   void set(const std::string& data);
  272.   std::string get() const { return get_cstring(); }
  273. };
  274.  
  275. /** Specialization for UTF-8 strings.
  276.  * @ingroup glibmmValue
  277.  */
  278. template <>
  279. class Value<Glib::ustring> : public ValueBase_String
  280. {
  281. public:
  282.   typedef Glib::ustring CppType;
  283.  
  284.   void set(const Glib::ustring& data);
  285.   Glib::ustring get() const { return get_cstring(); }
  286. };
  287.  
  288.  
  289. /** Base class of Glib::Value<T> specializations for enum types.
  290.  * @ingroup glibmmValue
  291.  */
  292. template <class T>
  293. class Value_Enum : public ValueBase_Enum
  294. {
  295. public:
  296.   typedef T CppType;
  297.  
  298.   void set(CppType data) { set_enum(data); }
  299.   CppType get() const    { return CppType(get_enum()); }
  300. };
  301.  
  302. /** Base class of Glib::Value<T> specializations for flags types.
  303.  * @ingroup glibmmValue
  304.  */
  305. template <class T>
  306. class Value_Flags : public ValueBase_Flags
  307. {
  308. public:
  309.   typedef T CppType;
  310.  
  311.   void set(CppType data) { set_flags(data); }
  312.   CppType get() const    { return CppType(get_flags()); }
  313. };
  314.  
  315. } // namespace Glib
  316.  
  317.  
  318. #endif /* _GLIBMM_VALUE_H */
  319.  
  320.